using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using Legacy.Core.SaveGameManagement;

namespace MMXSaveGameConverter
{
    internal sealed class SaveGameViewItem : UIElement
    {
        public static readonly DependencyProperty FullPathProperty = DependencyProperty.Register("FullPath", typeof(string), typeof(SaveGameViewItem), new PropertyMetadata(default(string)));
        public static readonly DependencyProperty DirectoryProperty = DependencyProperty.Register("Directory", typeof(string), typeof(SaveGameViewItem), new PropertyMetadata(default(string)));
        public static readonly DependencyProperty FileProperty = DependencyProperty.Register("File", typeof(string), typeof(SaveGameViewItem), new PropertyMetadata(default(string)));
        public static readonly DependencyProperty FormatProperty = DependencyProperty.Register("Format", typeof(SaveGameFormat), typeof(SaveGameViewItem), new PropertyMetadata(default(SaveGameFormat)));
        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(SaveGameViewItem), new PropertyMetadata(default(string)));
        public static readonly DependencyProperty SaveTimeProperty = DependencyProperty.Register("SaveTime", typeof(DateTime), typeof(SaveGameViewItem), new PropertyMetadata(default(DateTime)));
        public static readonly DependencyProperty GameTimeProperty = DependencyProperty.Register("GameTime", typeof(TimeSpan), typeof(SaveGameViewItem), new PropertyMetadata(default(TimeSpan)));
        public static readonly DependencyProperty DifficultyProperty = DependencyProperty.Register("Difficulty", typeof(string), typeof(SaveGameViewItem), new PropertyMetadata(default(string)));
        public static readonly DependencyProperty SaveGameProperty = DependencyProperty.Register("SaveGame", typeof(SaveGame), typeof(SaveGameViewItem), new PropertyMetadata(default(SaveGame)));
        public static readonly DependencyProperty ScreenshotProperty = DependencyProperty.Register("Screenshot", typeof(BitmapFrame), typeof(SaveGameViewItem), new PropertyMetadata(default(BitmapFrame)));

        public string FullPath
        {
            get { return (string)GetValue(FullPathProperty); }
            set { SetValue(FullPathProperty, value); }
        }

        public string Directory
        {
            get { return (string)GetValue(DirectoryProperty); }
            set { SetValue(DirectoryProperty, value); }
        }

        public string File
        {
            get { return (string)GetValue(FileProperty); }
            set { SetValue(FileProperty, value); }
        }

        public SaveGameFormat Format
        {
            get { return (SaveGameFormat)GetValue(FormatProperty); }
            set { SetValue(FormatProperty, value); }
        }

        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        public DateTime SaveTime
        {
            get { return (DateTime)GetValue(SaveTimeProperty); }
            set { SetValue(SaveTimeProperty, value); }
        }

        public TimeSpan GameTime
        {
            get { return (TimeSpan)GetValue(GameTimeProperty); }
            set { SetValue(GameTimeProperty, value); }
        }

        public string Difficulty
        {
            get { return (string)GetValue(DifficultyProperty); }
            set { SetValue(DifficultyProperty, value); }
        }

        public BitmapFrame Screenshot
        {
            get { return (BitmapFrame)GetValue(ScreenshotProperty); }
            set { SetValue(ScreenshotProperty, value); }
        }

        public SaveGame SaveGame
        {
            get { return (SaveGame)GetValue(SaveGameProperty); }
            set { SetValue(SaveGameProperty, value); }
        }

        public FileReaderBase Reader { get; set; }

        public void SetMeta(SaveFileMeta fileMeta, SaveGameMeta saveMeta)
        {
            Name = fileMeta.Name;
            SaveTime = saveMeta.Time;
            GameTime = saveMeta.PlayTime;
            Difficulty = saveMeta.Difficulty.ToString();
        }

        public void OnSelect()
        {
            using (IReaderState state = Reader.Read())
            {
                SaveFileMeta fileMeta = Reader.ReadFileMeta(state);
                SaveGameMeta saveMeta = Reader.ReadSaveMeta(state);
                SaveGame saveGame = Reader.ReadSaveGame(state, saveMeta);
                BitmapFrame screenshot = Reader.ReadScreenshot(state);

                SetMeta(fileMeta, saveMeta);
                SaveGame = saveGame;
                Screenshot = screenshot;
            }
        }

        public void OnDeselect()
        {
            SaveGame = null;
            Screenshot = null;
        }
    }
}
